package com.cq.cqmall.search.controller;

import com.cq.common.exception.BusinessCode;
import com.cq.common.to.es.SkuEsModel;
import com.cq.common.utils.R;
import com.cq.cqmall.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * 弹性搜索节省控制器
 *
 * @author 程崎
 * @since 2022/10/11
 */
@RestController
@RequestMapping("/search/save")
@Slf4j
public class ElasticSearchSaveController {

    @Resource
    private ProductSaveService productSaveService;

    /**
     * 产品状态
     * 上架商品
     *
     * @param skuEsModels sku es模型
     * @return {@link R}<{@link Object}>
     */
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModels) {
        boolean b;
        try {
            b = productSaveService.productStatusUp(skuEsModels);
        } catch (IOException e) {
            log.error("elasticsearch上架商品错误：{}", e);
            return R.error(BusinessCode.PRODUCT_UP_EXCEPTION.getCode(), BusinessCode.PRODUCT_UP_EXCEPTION.getMsg());
        }
        if (b) {
            return R.ok();
        }
        return R.error(BusinessCode.PRODUCT_UP_EXCEPTION.getCode(), BusinessCode.PRODUCT_UP_EXCEPTION.getMsg());
    }
}
